package com.bjb.vr.remote.ws.websocket.websocket;

import com.bjb.vr.common.utils.DateTimeUtil;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Map;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/6/13
 */
@Slf4j
public class ExamineHeartThread implements Runnable{

    @Override
    public void run() {
        while (true) {
            try {
                long timeMillins=System.currentTimeMillis();
                Map<String, Map<String, WebSocketServer>> classMap = WebSocketServer.classMap;
//                log.info("classMap.size() " + classMap.size());
                if(classMap.size()<=0){
                    Thread.sleep(1000);
                    continue;
                }
//                log.info("classMap " + classMap);
                classMap.entrySet().forEach(entry->{
                     Map map = entry.getValue();
//                    log.info("map.size() " + map.size());
                     if(map.size()!=0) {
                         map.forEach((key, value) -> {
                             WebSocketServer server = (WebSocketServer) value;
//                             log.info(Thread.currentThread().getName() + " " + "groupCode " + server.getGroupCode() + " udid " + server.getDeviceId());
//                             log.info(Thread.currentThread().getName() + " " + "server.isHeart() " + server.isHeart());
//                             log.info(Thread.currentThread().getName() + " " + "timeMillins " + timeMillins + " " + DateTimeUtil.timeStampToDateStr(timeMillins));
//                             log.info(Thread.currentThread().getName() + " " + "server.getHeartTime() " + server.getHeartTime() + " " + DateTimeUtil.timeStampToDateStr(server.getHeartTime()));
//                             log.info(Thread.currentThread().getName() + " " + "========================================================");

                             if (!server.isHeart() && server.getHeartTime() != 0 && timeMillins > server.getHeartTime() && !server.isAdmin()) {
                                     server.setKick(true);
                                     server.session.close();
                             }
                         });
                     }
                 });
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
